/*
 * Copyright (C) 2021 xuexiangjys(xuexiangjys@163.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.dona.imsidetector.fragment;

import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;

import com.dona.imsidetector.Dialog.Freq.SelectFreqDialog;
import com.dona.imsidetector.R;
import com.dona.imsidetector.db.dao.FreqDao;
import com.dona.imsidetector.db.dao.HistoryDao;
import com.dona.imsidetector.db.entity.Frequency;
import com.dona.imsidetector.db.entity.History;
import com.xuexiang.xui.utils.StatusBarUtils;


import java.util.List;
import java.util.Set;

public class SelectFreqDialogFragment extends DialogFragment {

    private static final String TAG = "AddFreqDialogFragment";

    private SelectFreqDialog custom_select_freq_dialog;
    private Integer select_isp = 1;
    private Integer period;

    public SelectFreqDialogFragment(Integer select_isp, Integer period) {
        this.select_isp = select_isp;
        this.period = period;
    }


    private DialogFragmentListener dialogFragmentListener;
    public interface DialogFragmentListener{
        void onDismiss();
    }

    public void setDialogFragmentListener(DialogFragmentListener dialogFragmentListener) {
        this.dialogFragmentListener = dialogFragmentListener;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @NonNull
    @Override
    public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {

        initDbDevices();
        initSelectDialog();


        return custom_select_freq_dialog;

    }




    private void appendFreq(List<Frequency> store, List<Frequency> freqs) {
        if (freqs == null && freqs.size() == 0) {
            return;
        }
        store.clear();
        store.addAll(freqs);
    }


    private void initSelectDialog() {

        custom_select_freq_dialog = new SelectFreqDialog(getActivity(), R.layout.dialog_my_custom, R.style.MyDialog);

        custom_select_freq_dialog.getLselected().clear();
        custom_select_freq_dialog.getRselected().clear();
        try {
            History history1 = HistoryDao.find(select_isp, 0);

            if (history1 != null) {
                if (history1.getFreqs() != null) {
                    Set<Integer> fromJson = History.freqsFromJson(history1.getFreqs());
                    custom_select_freq_dialog.getLselected().addAll(fromJson);
                    Log.e(TAG, "getLSelected: " + custom_select_freq_dialog.getLselected());
                }

            } else {
                Log.e(TAG, "history1 is null : ");
            }

            History history2 = HistoryDao.find(select_isp, 1);

            if (history2 != null) {
                if (history2.getFreqs() != null) {
                    Set<Integer> fromJson = History.freqsFromJson(history2.getFreqs());
                    custom_select_freq_dialog.getRselected().addAll(fromJson);
                    Log.e(TAG, "getRSelected: " + custom_select_freq_dialog.getLselected());
                }

            } else {
                Log.e(TAG, "history1 is null : ");
            }
        } catch (Exception e) {
            Log.e(TAG, "onClick: ", e);
        }
        switch (select_isp) {
            // 移动
            case 1:

                appendFreq(custom_select_freq_dialog.getLfrequencyList(), FreqDao.findByIspAndBands(select_isp, new String[]{"B39", "B41"}));
                Log.e(TAG, "custom_select_freq_dialog: " + custom_select_freq_dialog.getLfrequencyList());
                appendFreq(custom_select_freq_dialog.getRfrequencyList(), FreqDao.findByIspAndBands(select_isp, new String[]{"B39", "B40", "B41", "B3", "B5", "B8"}));

                break;
            case 2:
                appendFreq(custom_select_freq_dialog.getLfrequencyList(), FreqDao.findByIspAndBands(select_isp, new String[]{"B1", "B3", "B8"}));
                appendFreq(custom_select_freq_dialog.getRfrequencyList(), FreqDao.findByIspAndBands(select_isp, new String[]{"B1", "B3", "B8"}));
                break;
            case 3:
                appendFreq(custom_select_freq_dialog.getLfrequencyList(), FreqDao.findByIspAndBands(select_isp, new String[]{"B1", "B3", "B5"}));
                appendFreq(custom_select_freq_dialog.getRfrequencyList(), FreqDao.findByIspAndBands(select_isp, new String[]{"B1", "B3", "B5"}));
                break;
            case 4:

                appendFreq(custom_select_freq_dialog.getLfrequencyList(), FreqDao.findByInBands(new String[]{"B39", "B40", "B41"}));

                appendFreq(custom_select_freq_dialog.getRfrequencyList(), FreqDao.findByNotInBands(new String[]{"B39", "B40", "B41"}));

                break;
        }


        custom_select_freq_dialog.show();
        custom_select_freq_dialog.getLadapter().notifyDataSetChanged();
        custom_select_freq_dialog.getRadapter().notifyDataSetChanged();

        custom_select_freq_dialog.setOnPositiveListener(new SelectFreqDialog.PositiveListener() {
            @Override
            public void onClick(Set<Integer> selectLFreqs, Set<Integer> selectRFreqs) {
                HistoryDao.add(select_isp, 0, selectLFreqs);
                HistoryDao.add(select_isp, 1, selectRFreqs);
//                EventBus.getDefault().post(new SelectFreqEvent(selectLFreqs, selectRFreqs, period));

                Log.e(TAG, "setOnPositiveListener: " + selectLFreqs.toString());
                Log.e(TAG, "setOnPositiveListener: " + selectRFreqs.toString());

            }
        });


    }


    public void initDbDevices() {
        List<Frequency> all = FreqDao.findAll();
        if (all == null) {


            // 移动设备1
            FreqDao.add(new Frequency("B3", 1, 1300));
            FreqDao.add(new Frequency("B3", 1, 1303));
            FreqDao.add(new Frequency("B3", 1, 1306));
            FreqDao.add(new Frequency("B3", 1, 1375));
            FreqDao.add(new Frequency("B3", 1, 1275));
            FreqDao.add(new Frequency("B3", 1, 1350));
            FreqDao.add(new Frequency("B5", 1, 2452));
            FreqDao.add(new Frequency("B8", 1, 3683));
            FreqDao.add(new Frequency("B8", 1, 3590));
            FreqDao.add(new Frequency("B34", 1, 36275));
            FreqDao.add(new Frequency("B38", 1, 37900));
            FreqDao.add(new Frequency("B38", 1, 38098));
            FreqDao.add(new Frequency("B39", 1, 38400));
            FreqDao.add(new Frequency("B39", 1, 38544));
            FreqDao.add(new Frequency("B39", 1, 38350));
            FreqDao.add(new Frequency("B39", 1, 38496));
            FreqDao.add(new Frequency("B40", 1, 38950));
            FreqDao.add(new Frequency("B40", 1, 39148));
            FreqDao.add(new Frequency("B41", 1, 40936));
            FreqDao.add(new Frequency("B41", 1, 41134));
            FreqDao.add(new Frequency("B41", 1, 41140));
            FreqDao.add(new Frequency("B41", 1, 41332));


            //联通电信
            FreqDao.add(new Frequency("B1", 2, 500));
            FreqDao.add(new Frequency("B1", 2, 350));
            FreqDao.add(new Frequency("B1", 2, 400));

            FreqDao.add(new Frequency("B3", 2, 1650));
            FreqDao.add(new Frequency("B3", 2, 1525));
            FreqDao.add(new Frequency("B8", 2, 3715));
            FreqDao.add(new Frequency("B8", 2, 3740));


            FreqDao.add(new Frequency("B1", 3, 100));
            FreqDao.add(new Frequency("B3", 3, 1850));
            FreqDao.add(new Frequency("B3", 3, 1825));
            FreqDao.add(new Frequency("B5", 3, 2452));


        }


    }

    @Override
    public void onStart() {
        super.onStart();
//        Log.e("onStart", "" + getShowsDialog());
        try {
            Dialog dialog = getDialog();
            WindowManager.LayoutParams attributes = dialog.getWindow().getAttributes();
            Log.e(TAG, "onStart: " + attributes.width + " " + attributes.height);
//            attributes.width = 800;
//            attributes.height = 1200;
            dialog.getWindow().setAttributes(attributes);
        } catch (Exception e) {
            Log.e("onStart", "error: ", e);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        StatusBarUtils.fullScreen(getActivity());
        if(this.dialogFragmentListener != null){
            this.dialogFragmentListener.onDismiss();
        }


    }


}
